package orders

import (
	"api/internal/svc"
	"api/internal/types"
	"context"
	"order_srv/orderclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateOrderLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateOrderLogic {
	return &CreateOrderLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateOrderLogic) CreateOrder(req *types.CreateOrderRequest) (resp *types.Response, err error) {
	create, err := l.svcCtx.OrderRpc.OrderCreate(l.ctx, &orderclient.OrderCreateRequest{ //创建订单
		UserId:    uint32(req.UserId),
		GoodSkuId: uint32(req.GoodSkuId),
		AddressId: uint32(req.AddRessId),
		RealName:  req.RealName,
		UserPhone: req.UserPhone,
		TotalNum:  uint32(req.TotalNum),
		PayType:   req.PayType,
	})
	if err != nil {
		return &types.Response{ //创建订单失败
			Code:    500,
			Message: err.Error(),
		}, nil
	}
	return &types.Response{ //创建订单成功
		Code:    200,
		Message: "success",
		Data:    create,
	}, nil
}
